package com.dick.base.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 累加器
     * @param key
     * @param time
     * @return
     */
    public Long increase(String key, LocalDateTime time) {
        Long value = redisTemplate.opsForValue().increment(key);
        if (time != null) {
            redisTemplate.expireAt(key, time.toInstant(ZoneOffset.ofHours(8)));
        }
        return value;
    }
}
